package com.weitu.threeSimultaneities.fire.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.threeSimultaneities.fire.entity.Fire;
import com.weitu.threeSimultaneities.fire.entity.FireAcceptance;
import com.weitu.threeSimultaneities.fire.entity.FireDesign;
import com.weitu.threeSimultaneities.fire.service.FireAcceptanceService;
import com.weitu.threeSimultaneities.fire.service.FireDesignService;
import com.weitu.threeSimultaneities.fire.service.FireService;
import com.weitu.threeSimultaneities.projectApproval.entity.ProjectApproval;
import com.weitu.threeSimultaneities.projectApproval.entity.ProjectSpecialty;
import com.weitu.threeSimultaneities.projectApproval.service.ProjectApprovalService;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/5/24.
 */
@Controller
@RequestMapping(value = "threeSimultaneities/fire")
public class FireController  extends BaseController{

    @Autowired
    private AccessoryUtilService accessoryUtilService;

    @Autowired
    private FireService fireService;

    @Autowired
    private FireDesignService fireDesignService;

    @Autowired
    private FireAcceptanceService fireAcceptanceService;

    @Autowired
    private ProjectApprovalService projectApprovalService;

    /**
     * 打开编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editAjax(Model model, String id, String projectId,String type){

        //消防专业主数据
        Fire fire=fireService.selectByPrimaryKey(id);
        //主项目信息
        ProjectApproval projectApproval=projectApprovalService.selectByProjectId(projectId);
        //对项目专业ID进行拼接
        if(projectApproval.getProjectSpecialtyList().size()>0){
            String SpecialtyList="";
            for(ProjectSpecialty projectSpecialty:projectApproval.getProjectSpecialtyList()){
                SpecialtyList+=","+projectSpecialty.getSpecialtyId();
            }
            SpecialtyList=SpecialtyList.substring(1);
            model.addAttribute("SpecialtyList",SpecialtyList);
        }
        //项目主信息附件
        List<AccessoryPackageInfo> proAccessoryList = accessoryUtilService.queryAccessoryByBusinessId(projectApproval.getId().toString());
        //消防专业所有附件
        List<AccessoryPackageInfo> fireAccessoryList = accessoryUtilService.queryAccessoryByBusinessId(id);
        //设计阶段
        FireDesign fireDesign=fireDesignService.selectBySubjectId(id);
        if(fireDesign!=null){ model.addAttribute("fireDesign", fireDesign);}
        //验收阶段
        FireAcceptance fireAcceptance=fireAcceptanceService.selectBySubjectId(id);
        if(fireAcceptance!=null){model.addAttribute("fireAcceptance",fireAcceptance);}

        model.addAttribute("fire", fire);
        model.addAttribute("fireAccessoryList", fireAccessoryList);
        model.addAttribute("proAccessoryList", proAccessoryList);
        model.addAttribute("projectApproval", projectApproval);
        return BASE_JSP_PATH + "threeSimultaneities/fire/"+type;
    }


    /**
     * 添加项设计阶段信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "消防三同时", methods = "添加设计阶段信息")
    public AjaxResult insertSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState", 0);
        int boo = fireDesignService.insertSelective(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("subjectId").toString());
        if (boo >0&& accFlag) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 添加项验收阶段信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addTwo", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "消防三同时", methods = "添加验收阶段信息")
    public AjaxResult insertSelectivet() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState", 0);
        int boo = fireAcceptanceService.insertSelective(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("subjectId").toString());
        if (boo >0&& accFlag) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }
}
